package cn.xmy.demo01.servlet;

import cn.xmy.demo01.pojo.Address;
import cn.xmy.demo01.service.AddressService;
import cn.xmy.demo01.service.impl.AddressServiceImpl;
import com.alibaba.fastjson.JSON;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/findAddressServlet")
public class FindAddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AddressService addressService=new AddressServiceImpl();
        List<Address> addresses=null;
        try {
            addresses=addressService.findAllAddress();
            String jsonBook= JSON.toJSONString(addresses);
            PrintWriter out=response.getWriter();
            out.print(jsonBook);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}